package cn.wenhe9.yygh.hosp.service.impl;

import cn.wenhe9.yygh.common.exception.YyghException;
import cn.wenhe9.yygh.common.result.ResultCodeEnum;
import cn.wenhe9.yygh.common.result.ResultResponse;
import cn.wenhe9.yygh.hosp.mapper.HospitalSetMapper;
import cn.wenhe9.yygh.hosp.service.HospitalSetService;
import cn.wenhe9.yygh.model.hosp.HospitalSet;
import cn.wenhe9.yygh.model.vo.hosp.HospitalSetQueryVo;
import cn.wenhe9.yygh.model.vo.order.SignInfoVo;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Random;


/**
 * @author DuJinliang
 * 2022/2/10
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    @Override
    public ResultResponse<List<HospitalSet>> findAllHospSet() {
        List<HospitalSet> hospitalSetList = this.list();
        return ResultResponse.ok(hospitalSetList);
    }

    @Override
    public ResultResponse removeHospSet(Long id) {
        boolean removeFlag = this.removeById(id);
        if (removeFlag){
            return ResultResponse.ok();
        }
        return ResultResponse.fail();
    }

    @Override
    public ResultResponse<Page<HospitalSet>> findHospSetByPage(Integer current, Integer limit, HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);

        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();

        //调用方法实现分页查询
        Page<HospitalSet> hospitalSetPage = this.query()
                .like(StringUtils.isNotEmpty(hosname), "hosname", hosname)
                .eq(StringUtils.isNotEmpty(hoscode), "hoscode", hoscode)
                .page(page);

        return ResultResponse.ok(hospitalSetPage);
    }

    @Override
    public ResultResponse saveHospSet(HospitalSet hospitalSet) {
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);

        //签名秘钥
        Random random = new Random();
        String str = System.currentTimeMillis() + "" + random.nextInt(10000);
        hospitalSet.setSignKey(MD5Utils.encodeHexString(str.getBytes()));

        boolean saveFlag = this.save(hospitalSet);

        if (saveFlag){
            return ResultResponse.ok();
        }else {
            return ResultResponse.fail();
        }
    }

    @Override
    public ResultResponse getHospSetById(Long id) {
        HospitalSet hospitalSet = this.getById(id);
        return ResultResponse.ok(hospitalSet);
    }

    @Override
    public ResultResponse updateHospSetById(HospitalSet hospitalSet) {
        boolean updateFlag = this.updateById(hospitalSet);
        if (updateFlag){
            return ResultResponse.ok();
        }
        return ResultResponse.fail();
    }

    @Override
    public ResultResponse batchRemoveHospSetById(List<Long> idList) {
        boolean removeFlag = this.removeByIds(idList);
        if (removeFlag){
            return ResultResponse.ok();
        }
        return ResultResponse.fail();
    }

    @Override
    public ResultResponse lockHospSetById(Long id, Integer status) {
        boolean lockFlag = this.update()
                .set("status", status)
                .eq("id", id)
                .update();
        if (lockFlag){
            return ResultResponse.ok();
        }
        return ResultResponse.fail();
    }

    @Override
    public ResultResponse sendKey(Long id) {
        HospitalSet hospitalSet = this.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return null;
    }

    @Override
    public String getSignKey(String hoscode) {
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hoscode", hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(queryWrapper);

        return hospitalSet.getSignKey();
    }

    @Override
    public void checkSignKey(Map<String, Object> paramMap) {

        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //获取医院系统传递过来的签名，签名进行Md5加密
        String hospSign = (String) paramMap.get("sign");

        //根据传递过来的医院编码，查询数据库，查询签名
        String signKey = this.getSignKey(hoscode);

        //把数据库中查出的签名进行加密
        String signKeyMD5 = DigestUtils.md5Hex(signKey.getBytes());

        //判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
    }

    @Override
    public SignInfoVo getSignInfo(String hoscode) {
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hoscode", hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(queryWrapper);
        if (null == hospitalSet){
            throw new YyghException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        signInfoVo.setSignKey(hospitalSet.getSignKey());

        return signInfoVo;
    }

}
